<template>
  <el-card>
    <template #header>商品统计</template>

    <div>
      <!-- 1.0 查询的表单 -->
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        size="mini"
      >
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 2.0 显示的图表 -->
      <Chart :option="option" :info="linedata" :str="'goods'" />
    </div>
  </el-card>
</template>

<script>
import option from './option.js'
import { getOrderData } from '@/api/charts.js'
import moment from 'moment'

export default {
  data() {
    return {
      searchForm: {
        date: []
      },
      // eachs图表配置项数据
      option,
      // 折线图的数据
      linedata: {}
    }
  },
  mounted() {
    this.getDatas()
  },
  methods: {
    async getDatas() {
      const { data } = await getOrderData({
        date: JSON.stringify(this.searchForm.date)
      })
      const obj = {}
      data.forEach(item => {
        item.orderTime = moment(item.orderTime).format('MM-DD')
        if (obj[item.orderTime]) {
          obj[item.orderTime] += item.orderAmount
        } else {
          obj[item.orderTime] = item.orderAmount
        }
      })
      console.log(obj)
      this.linedata = obj
    },
    onSubmit() {}
  }
}
</script>

<style lang="less" scoped></style>
